package com.pptree.service.impl;

import java.util.List;

import com.pptree.dao.GenreDAO;
import com.pptree.dao.impl.GenreDAOImpl;
import com.pptree.service.GenreService;
import com.pptree.vo.Genre;

public class GenreServiceImpl implements GenreService {
	
	public GenreDAO genreDAOImpl = new GenreDAOImpl();
	
	@Override
	public boolean addCategory(Genre genre) {
		try {
			Genre g = getGenreByName(genre.getName());
			if(g != null){
				return false;
			}
			else{
				return genreDAOImpl.addGenre(genre) > 0;
			}
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public boolean modifyCategory(Genre genre) {
		
		try {
			Genre g = genreDAOImpl.findGenreById(genre.getId());
			boolean result = genreDAOImpl.updExistsName(g.getName(), genre.getName());
			if(result){
				return false;
			}
			else{
				return genreDAOImpl.updGenre(genre) > 0;
			}
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public boolean deleteCategory(int id) {
		try {
			return genreDAOImpl.delGenre(id) > 0;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public List<Genre> getAllCategories() {
		try {
			return genreDAOImpl.findAllGenre();
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Genre getGenreById(int id) {
		try {
			return genreDAOImpl.findGenreById(id);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Genre getGenreByName(String name) {
		
		try {
			return genreDAOImpl.findGenreByName(name);
		} catch (Exception e) {
			throw e;
		}
	}

}
